//
//  LGImageManagerIO.m
//  LuxuryGoods
//
//  Created by 刘星辰 on 2018/5/19.
//  Copyright © 2018年 Future. All rights reserved.
//

#import "LGImageManagerIO.h"
#import "NSString+Tool.h"


@implementation LGImageManagerIO

/**
 获取缩略图
 
 @param asset 图像资源
 @param synchronous 是否同步线程
 @param size 尺寸
 @param resultHandler 结果block
 */
+ (void)thumbnailForAsset:(PHAsset *)asset synchronous:(BOOL)synchronous size:(CGSize)size resultHandler:(void(^)(UIImage *result))resultHandler
{
    //尺寸
    CGFloat scale = [UIScreen mainScreen].scale;
    CGSize realitySize = CGSizeMake(size.width * scale, size.height *scale);
    PHImageRequestOptions *opts = [[PHImageRequestOptions alloc] init];
    opts.resizeMode = PHImageRequestOptionsResizeModeFast;
    opts.synchronous = synchronous;
    [[PHImageManager defaultManager] requestImageForAsset:asset targetSize:realitySize contentMode:PHImageContentModeAspectFill options:opts resultHandler:^(UIImage * _Nullable result, NSDictionary * _Nullable info) {
        if (resultHandler)
        {
            resultHandler(result);
        }
    }];
}

/**
 获取缩略图
 */
+ (void)thumbnailForAsset:(PHAsset *)asset size:(CGSize)size resultHandler:(void(^)(UIImage *result))resultHandler
{
    [[self class] thumbnailForAsset:asset synchronous:NO size:size resultHandler:resultHandler];
}

/**
 获取缩略图 同步方法
 
 @param asset 资源
 @param size 具体屏幕尺寸
 @return 图片
 */
+ (UIImage *)thumbnailForAsset:(PHAsset *)asset size:(CGSize)size
{
    __block UIImage *image = nil;
    [[self class] thumbnailForAsset:asset synchronous:YES size:size resultHandler:^(UIImage *result) {
        image = result;
    }];
    
    return image;
}



/**
 获取原图
 
 @param asset 图像资源
 @param synchronous 是否同步线程
 @param resultHandler 结果block
 */
+ (void)originalForAsset:(PHAsset *)asset  synchronous:(BOOL)synchronous resultHandler:(void(^)(UIImage *result))resultHandler
{
    CGSize size = CGSizeMake(asset.pixelWidth, asset.pixelHeight);
    PHImageRequestOptions *opts = [[PHImageRequestOptions alloc] init];
    opts.resizeMode = PHImageRequestOptionsResizeModeExact;
    opts.deliveryMode = PHImageRequestOptionsDeliveryModeHighQualityFormat;
    opts.synchronous = synchronous;
    
    [[PHImageManager defaultManager] requestImageForAsset:asset targetSize:size contentMode:PHImageContentModeAspectFill options:opts resultHandler:^(UIImage * _Nullable result, NSDictionary * _Nullable info) {
        if (resultHandler)
        {
            resultHandler(result);
        }
    }];
}

/**
 获取原图
 */
+ (void)originalForAsset:(PHAsset *)asset resultHandler:(void(^)(UIImage *result))resultHandler
{
    [[self class] originalForAsset:asset synchronous:NO resultHandler:resultHandler];
}


/**
 获取原图 同步方法
 */
+ (UIImage *)originalForAsset:(PHAsset *)asset
{
    __block UIImage *image = nil;
    [self originalForAsset:asset synchronous:YES  resultHandler:^(UIImage *result) {
        image = result;
    }];
    return image;
}

+ (UIImage *)addText:(UIImage *)image text:(NSString *)text
{
    int width = image.size.width;
    int height = image.size.height;
    
    
    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
    
    CGContextRef context = CGBitmapContextCreate(NULL, width, height, 8, 4 * width, colorSpace, kCGImageAlphaPremultipliedFirst);
    
    
    CGContextDrawImage(context, CGRectMake(0, 0, width, height), image.CGImage);
    
    CGContextSetRGBFillColor(context, 0.0, 0.0, 0.0, 1);
    
    
    char* chatText  = (char *)[text cStringUsingEncoding:NSASCIIStringEncoding];
    
    CGContextSelectFont(context, "Arial-BoldItalicMT", 50, kCGEncodingMacRoman);
    
    CGContextSetTextDrawingMode(context, kCGTextFill);
    
    CGContextSetRGBFillColor(context, 255, 255, 255, 1);
    
    
    //rotate text
    
    //    CGContextSetTextMatrix(context, CGAffineTransformMakeRotation( -M_PI/4 ));
    
    
    CGContextShowTextAtPoint(context, width/2 - 40, height/2-10, chatText, strlen(chatText));
    
    CGImageRef imageMasked = CGBitmapContextCreateImage(context);
    
    CGContextRelease(context);
    
    CGColorSpaceRelease(colorSpace);
    
    return [UIImage imageWithCGImage:imageMasked];
    
}


#pragma mark - 在image上写文字
/**
 在image上写文字
 
 @param image 传进来的图片
 @param text 传进来的文本
 @param font 传进来的字体大小
 @return UIImage 带文字的Image
 */
+ (UIImage *)drawTextOnImage:(UIImage *)image text:(NSString *)text font:(UIFont *)font textColor:(UIColor*)color
{
    if (![text isNotBlank]) {
        return image;
    }
    CGSize sizeImg = image.size;
    
    CGSize sizeTextCanDraw = [text sizeWithAttributes:@{NSFontAttributeName : font}];
    int maxWidth = sizeImg.width;
    int maxHeight = sizeImg.height;
    
    CGFloat scale = [[UIScreen mainScreen] scale];
    UIGraphicsBeginImageContext(CGSizeMake(maxWidth*scale, maxHeight*scale));
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextScaleCTM(UIGraphicsGetCurrentContext(), scale, scale);
    
    CGRect rcImage = CGRectMake(0, 0, sizeImg.width, sizeImg.height);
    [image drawInRect:rcImage];
    // 绘制文字
    CGContextSetFillColorWithColor(context, color.CGColor);
    CGRect rcTextRect = CGRectMake(CGRectGetMaxX(rcImage)/2 - (sizeTextCanDraw.width/2), (sizeImg.height - sizeTextCanDraw.height)/2, sizeTextCanDraw.width, sizeTextCanDraw.height);
    [text drawInRect:rcTextRect withAttributes:@{NSFontAttributeName : font ,NSForegroundColorAttributeName : color}];
    
    UIImage *textImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    
    return textImage;
}

#pragma mark - 颜色生成图片
/**
 根据颜色生成图片
 
 @param color 传入的颜色
 @return UIImage 生成的图片
 */
+ (UIImage*)createImageWithColor:(UIColor*) color
{
    CGRect rect=CGRectMake(0.0f, 0.0f, 1.0f, 1.0f);
    UIGraphicsBeginImageContext(rect.size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetFillColorWithColor(context, [color CGColor]);
    CGContextFillRect(context, rect);
    UIImage *theImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return theImage;
}


#pragma mark - 生成横向渐变色层
/**
 生成横向渐变图片
 
 @param size 渐变层尺寸
 @param startColor 开始颜色
 @param endColor 结束颜色
 @return 渐变层
 */
+ (UIImage *)createGradualColorImage:(CGSize)size startColor:(UIColor *)startColor endColor:(UIColor *)endColor
{
    
    NSAssert((size.height != 0 )&&(size.width != 0 ), @"目标视图frame没有设值");
    
    
    CAGradientLayer *gradientLayer = [CAGradientLayer layer];
    
    gradientLayer.colors = @[(__bridge id)startColor.CGColor,(__bridge id)endColor.CGColor];
    
    gradientLayer.locations = @[@0.3,@1.0];
    
    gradientLayer.startPoint = CGPointMake(0, 0);
    
    gradientLayer.endPoint = CGPointMake(1.0, 0);
    
    gradientLayer.frame = CGRectMake(0, 0, size.width, size.height);
    
    // 第二个参数 NO代表为透明
    UIGraphicsBeginImageContextWithOptions(gradientLayer.bounds.size, NO, [UIScreen mainScreen].scale);
    
    [gradientLayer renderInContext:UIGraphicsGetCurrentContext()];
    
    UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
    
    UIGraphicsEndImageContext();
    
    return img;
}
@end
